
import sys
print('star ')
sys.path.append('/kaggle/working/stable-diffusion-webui/modules')  # 替换为实际的模块所在路径
sys.path.append('/kaggle/working/stable-diffusion-webui')  # 替换为实际的模块所在路径



def import_from():
    print("import_from")
    try:
        import scripts
        print("import_from scripts")
        print("from . import sd_samplers, processing")
        # from . import sd_samplers, processing
        # import sd_samplers, processing
        import sd_samplers
        import processing
        from generation_parameters_copypaste import create_override_settings_dict
        from shared import opts, cmd_opts
        import shared 
        # import shared as shared
        from ui import plaintext_to_html
    except Exception as e:
        print(e)
        # import scripts
        # from . import sd_samplers, processing
        # from .generation_parameters_copypaste import create_override_settings_dict
        # from .shared import opts, cmd_opts
        # import .shared as shared
        # from .ui import plaintext_to_html


try:
    print('try import modules.scripts')
    import modules.scripts
    from modules import sd_samplers, processing
    from modules.generation_parameters_copypaste import create_override_settings_dict
    from modules.shared import opts, cmd_opts
    import modules.shared as shared
    from modules.ui import plaintext_to_html
except Exception as e:
    print(e)
    # print(e)
    # # import .scripts
    # import scripts
    # from . import sd_samplers, processing
    # from .generation_parameters_copypaste import create_override_settings_dict
    # from .shared import opts, cmd_opts
    # import .shared as shared
    # from .ui import plaintext_to_html
    import_from()

# import scripts
# print("import_from scripts")
# print("from . import sd_samplers, processing")
# # from . import sd_samplers, processing
# # import sd_samplers, processing
# import sd_samplers
# import processing
# from generation_parameters_copypaste import create_override_settings_dict
# from shared import opts, cmd_opts
# import shared 
# # import shared as shared
# from ui import plaintext_to_html


def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, steps: int, sampler_index: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, enable_hr: bool, denoising_strength: float, hr_scale: float, hr_upscaler: str, hr_second_pass_steps: int, hr_resize_x: int, hr_resize_y: int, hr_sampler_index: int, hr_prompt: str, hr_negative_prompt, override_settings_texts, *args):
    override_settings = create_override_settings_dict(override_settings_texts)
    # print("id_task")
    # print(id_task)
    # print("prompt")
    # print(prompt)
    # print("negative_prompt")
    # print(negative_prompt)
    # print("prompt_styles")
    # print(prompt_styles)
    # print("steps")
    # print(steps)
    # print("sampler_index")
    # print(sampler_index)
    # print("restore_faces")
    # print(restore_faces)
    # print("tiling")
    # print(tiling)
    # print("n_iter")
    # print(n_iter)
    # print("batch_size")
    # print(batch_size)
    # print("cfg_scale")
    # print(cfg_scale)

    print("id_task:", id_task)
    print("prompt:", prompt)
    print("negative_prompt:", negative_prompt)
    print("prompt_styles:", prompt_styles)
    print("steps:", steps)
    print("sampler_index:", sampler_index)
    print("restore_faces:", restore_faces)
    print("tiling:", tiling)
    print("n_iter:", n_iter)
    print("batch_size:", batch_size)
    print("cfg_scale:", cfg_scale)
    print("seed:", seed)
    print("subseed:", subseed)
    print("subseed_strength:", subseed_strength)
    print("seed_resize_from_h:", seed_resize_from_h)
    print("seed_resize_from_w:", seed_resize_from_w)
    print("seed_enable_extras:", seed_enable_extras)
    print("height:", height)
    print("width:", width)
    print("enable_hr:", enable_hr)
    print("denoising_strength:", denoising_strength)
    print("hr_scale:", hr_scale)
    print("hr_upscaler:", hr_upscaler)
    print("hr_second_pass_steps:", hr_second_pass_steps)
    print("hr_resize_x:", hr_resize_x)
    print("hr_resize_y:", hr_resize_y)
    print("hr_sampler_index:", hr_sampler_index)
    print("hr_prompt:", hr_prompt)
    print("hr_negative_prompt:", hr_negative_prompt)
    print("override_settings_texts:", override_settings_texts)
    print("args:", args)
    print("opts:", opts)
    print("opts.outdir_txt2img_samples:", opts.outdir_txt2img_samples)
    print("shared.sd_model:", shared.sd_model)
    print("opts.outdir_samples:", opts.outdir_samples)
    print("opts.outdir_grids:", opts.outdir_grids)
    print("opts.outdir_grids:", opts.outdir_grids)
    print("sd_samplers.samplers[sampler_index].name",sd_samplers.samplers[sampler_index].name)
    print("sd_samplers.samplers[sampler_index].name",sd_samplers.samplers[sampler_index].name)
    if hr_sampler_index != 0:
        print("sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name",sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name)
    p = processing.StableDiffusionProcessingTxt2Img(
        sd_model=shared.sd_model,
        outpath_samples=opts.outdir_samples or opts.outdir_txt2img_samples,
        outpath_grids=opts.outdir_grids or opts.outdir_txt2img_grids,
        prompt=prompt,
        styles=prompt_styles,
        negative_prompt=negative_prompt,
        seed=seed,
        subseed=subseed,
        subseed_strength=subseed_strength,
        seed_resize_from_h=seed_resize_from_h,
        seed_resize_from_w=seed_resize_from_w,
        seed_enable_extras=seed_enable_extras,
        sampler_name=sd_samplers.samplers[sampler_index].name,
        batch_size=batch_size,
        n_iter=n_iter,
        steps=steps,
        cfg_scale=cfg_scale,
        width=width,
        height=height,
        restore_faces=restore_faces,
        tiling=tiling,
        enable_hr=enable_hr,
        denoising_strength=denoising_strength if enable_hr else None,
        hr_scale=hr_scale,
        hr_upscaler=hr_upscaler,
        hr_second_pass_steps=hr_second_pass_steps,
        hr_resize_x=hr_resize_x,
        hr_resize_y=hr_resize_y,
        hr_sampler_name=sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name if hr_sampler_index != 0 else None,
        hr_prompt=hr_prompt,
        hr_negative_prompt=hr_negative_prompt,
        override_settings=override_settings,
    )

    p.scripts = modules.scripts.scripts_txt2img
    p.script_args = args

    if cmd_opts.enable_console_prompts:
        print(f"\ntxt2img: {prompt}", file=shared.progress_print_out)

    processed = modules.scripts.scripts_txt2img.run(p, *args)

    if processed is None:
        processed = processing.process_images(p)

    p.close()

    shared.total_tqdm.clear()

    generation_info_js = processed.js()
    if opts.samples_log_stdout:
        print(generation_info_js)

    if opts.do_not_show_images:
        processed.images = []

    return processed.images, generation_info_js, plaintext_to_html(processed.info), plaintext_to_html(processed.comments)

def simple_txt2img_request():
    return {
        "batch_size": 1,
        "cfg_scale": 7,
        "denoising_strength": 0,
        "enable_hr": False,
        # "eta": 0,
        "firstphase_height": 0,
        "firstphase_width": 0,
        "height": 64,
        "n_iter": 1,
        "negative_prompt": "",
        "prompt": "example prompt",
        "restore_faces": False,
        "s_churn": 0,
        "s_noise": 1,
        "s_tmax": 0,
        "s_tmin": 0,
        "sampler_index": "Euler a",
        "seed": -1,
        "seed_resize_from_h": -1,
        "seed_resize_from_w": -1,
        "steps": 3,
        "styles": [],
        "subseed": -1,
        "subseed_strength": 0,
        "tiling": False,
        "width": 64,
    }



if __name__=="__main__":
    id_task="1"
    prompt="miku"
    negative_prompt="((badhandv4, easynegative, ng_deepnegative_v1_75t, verybadimagenegative_v1.3)), (monochrome, grayscale), text, watermark, bad anatomy"
    prompt_styles=None
    steps=20
    sampler_index=0
    restore_faces=True
    tiling=True
    n_iter=20
    batch_size=1
    # cfg_scale=0.1
    cfg_scale=1
    seed=-1
    subseed=-1
    subseed_strength=1
    # subseed_strength=1
    req=simple_txt2img_request()
    # id_task: str, prompt: str, negative_prompt: str, prompt_styles, steps: int, sampler_index: int, 
    # restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, 
    # cfg_scale: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, enable_hr: bool, denoising_strength: float, hr_scale: float, hr_upscaler: str, hr_second_pass_steps: int, hr_resize_x: int, hr_resize_y: int, hr_sampler_index: int, hr_prompt: str, hr_negative_prompt, override_settings_texts, *args
    txt2img(**req)